package com.goods.controller;

import com.goods.pojo.Order;
import com.goods.pojo.Page;
import com.goods.service.CartItemService;
import com.goods.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/admin/AdminOrderController")
public class AdminOrderController {
	
	@Resource
	private OrderService orderServiceImpl;
	
	@Resource
	private CartItemService cartItemServiceImpl;
	
	/**
	 * 获取当前页码
	 * @param req
	 * @return
	 */
	private int getPc(HttpServletRequest req) {
		int pc = 1;
		String param = req.getParameter("pc");
		
		if(param != null && !param.trim().isEmpty()) {
			try {
				pc = Integer.parseInt(param);
			} catch(RuntimeException e) {}
		}
		return pc;
	}

	// 截取url，分页导航中使用它做为超链接的目标
	/*
	 * http://localhost:8080/goods/BookController/findByCategory?cid=xxx&pc=3
	 * /goods/BookController/findByCategory + cid=xxx&pc=3
	 */
	private String getUrl(HttpServletRequest req) {
		String url = req.getRequestURI() + "?" + req.getQueryString();
		///goods/BookController/findByCategory?cid=5F79D0D246AD4216AC04E9C5FAB3199E++++++++++++++++++++++++++++
		System.out.println("getUrl()....................................");
		/*如果url中存在pc参数，就截取掉，否则截取。
		 * 	lastIndexOf : 从右到左，字符串出现的最后一次位置
		 *  */
		int index = url.lastIndexOf("&pc=");
		if(index != -1) {
			// 截取字符串 索引 0 - index
			url = url.substring(0, index);
		}
		return url;
	}
	
	// 查看所有订单
	@RequestMapping("/findAll")
	public String findAll(HttpServletRequest req, HttpServletResponse resp){
		int pc = getPc(req);
		String url = getUrl(req);
		Page<Order> pb = orderServiceImpl.findAll(pc);
		// 给PageBean设置url，保存PageBean
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "/adminjsps/admin/order/list.jsp";
	}

	// 按状态查询
	@RequestMapping("/findByStatus")
	public String findByStatus(HttpServletRequest req, HttpServletResponse resp){
		int pc = getPc(req);
		String url = getUrl(req);
		// 获取状态码
		int status = Integer.parseInt(req.getParameter("status"));
		Page<Order> pb = orderServiceImpl.findByStatus(status, pc);
		// 给PageBean设置url，保存PageBean
		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "/adminjsps/admin/order/list.jsp";
	}
	

	// 查看订单详细信息
	@RequestMapping("/load")
	public String load(HttpServletRequest req, HttpServletResponse resp){
		String oid = req.getParameter("oid");
		Order order = orderServiceImpl.load(oid);
		req.setAttribute("order", order);
		//btn说明用户点击哪个超链接来访问本方法
		String btn = req.getParameter("btn");
		req.setAttribute("btn", btn);
		return "/adminjsps/admin/order/desc.jsp";
	}
	
	// 取消订单
	@RequestMapping("/cancel")
	public String cancel(HttpServletRequest req, HttpServletResponse resp){
		String oid = req.getParameter("oid");
		// 校验订单状态
		int status = orderServiceImpl.findStatus(oid);
		if(status != 1) {
			req.setAttribute("code", "error");
			req.setAttribute("msg", "状态不对，不能取消！");
			return "/adminjsps/msg.jsp";
		}
		//设置状态为取消
		orderServiceImpl.updateStatus(oid, 5);
		req.setAttribute("code", "success");
		req.setAttribute("msg", "订单已取消");
		return "/adminjsps/msg.jsp";		
	}
	

	// 发货功能
	@RequestMapping("/deliver")
	public String deliver(HttpServletRequest req, HttpServletResponse resp){
		String oid = req.getParameter("oid");
		// 校验订单状态
		int status = orderServiceImpl.findStatus(oid);
		if(status != 2) {
			req.setAttribute("code", "error");
			req.setAttribute("msg", "状态不对，不能发货！");
			return "/adminjsps/msg.jsp";
		}
		// 设置为发货状态
		orderServiceImpl.updateStatus(oid, 3);
		req.setAttribute("code", "success");
		req.setAttribute("msg", "订单已发货，请查看物流，马上确认吧！");
		return "/adminjsps/msg.jsp";		
	}

}
